package com.ssyt.wxstore.sys.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ssyt.wxstore.common.BaseAction;
import com.ssyt.wxstore.common.PageModel;
import com.ssyt.wxstore.common.util.FastjsonUtil;
import com.ssyt.wxstore.common.util.MD5Util;
import com.ssyt.wxstore.sys.entity.SysMenu;
import com.ssyt.wxstore.sys.entity.SysRole;
import com.ssyt.wxstore.sys.entity.SysUser;
import com.ssyt.wxstore.sys.service.UserService;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService userService;
	private SysUser user;
	private List<SysMenu> menuList;
	private int pageNo;
	private PageModel<SysUser> pageModel;
	private List<SysUser> listUser;
	private List<String> roleId;
	private String id;

	/**
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception {
		user = userService.login(user.getCode(),
				MD5Util.toMD5(user.getPassword()));
		if (user != null) {
			getSession().setAttribute("user", user);
			return SUCCESS;
		} else {
			return "fail";
		}

	}

	/**
	 * 获取菜单
	 * 
	 * @throws Exception
	 */
	public void getMenu() throws Exception {
		user = (SysUser) getSession().getAttribute("user");
		menuList = userService.getMenuList(user.getId());
		print(FastjsonUtil.toJson(menuList));
	}

	/**
	 * 用户列表
	 * 
	 * @throws Exception
	 */
	public String getUserList() throws Exception {
		pageModel = userService.getUserList(pageNo);
		listUser = pageModel.getList();
		return "userList";
	}

	/**
	 * 查询角色
	 * 
	 * @throws Exception
	 */
	public void getRole() throws Exception {
		List<SysRole> roleList = userService.getRole();
		print(FastjsonUtil.toJson(roleList));
	}

	/**
	 * 新增用户
	 * 
	 * @throws Exception
	 */
	public void add() throws Exception {
		String rowId = userService.add(user, roleId);
		print(rowId);
	}

	/**
	 * 修改用户
	 * 
	 * @throws Exception
	 */
	public void update() throws Exception {
		int row = userService.update(user, roleId);
		print(row);
	}

	/**
	 * 获取用户角色
	 * 
	 * @throws Exception
	 */
	public void getUserRole() throws Exception {
		SysUser sysUser = userService.getUserRole(id);
		print(FastjsonUtil.toJson(sysUser));
	}

	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}

	public List<SysMenu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<SysMenu> menuList) {
		this.menuList = menuList;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public PageModel<SysUser> getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel<SysUser> pageModel) {
		this.pageModel = pageModel;
	}

	public List<SysUser> getListUser() {
		return listUser;
	}

	public void setListUser(List<SysUser> listUser) {
		this.listUser = listUser;
	}

	public void setRoleId(List<String> roleId) {
		this.roleId = roleId;
	}

	public List<String> getRoleId() {
		return roleId;
	}

	public void setId(String id) {
		this.id = id;
	}

}
